import 'package:flutter/material.dart';

class EpisodeGrid extends StatefulWidget {
  final List<Map<String, String>> episodes;
  final Function(String) onEpisodeTap; // 接收点击回调
  final selectedUrl;
  const EpisodeGrid({
    Key? key,
    required this.episodes,
    required this.onEpisodeTap,
    this.selectedUrl = "",
  }) : super(key: key);

  @override
  State<EpisodeGrid> createState() => _EpisodeGridState();
}

class _EpisodeGridState extends State<EpisodeGrid> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Wrap(
        spacing: 8.0, // 水平间距
        runSpacing: 4.0, // 垂直间距
        children: widget.episodes
            .map((item) => Label(
                  episode: item,
                  onTap: widget.onEpisodeTap,
                  selected: widget.selectedUrl == item['url'],
                ))
            .toList(),
      ),
    );
  }
}

class Label extends StatefulWidget {
  final Map<String, String> episode;
  final Function(String) onTap;
  final bool selected;
  const Label({
    Key? key,
    required this.episode,
    required this.onTap,
    this.selected = false,
  }) : super(key: key);

  @override
  State<Label> createState() => _LabelState();
}

class _LabelState extends State<Label> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => widget.onTap(widget.episode['url']!),
      child: Container(
        width: 68,
        padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
        decoration: BoxDecoration(
          color: Colors.grey[100],
          borderRadius: BorderRadius.circular(4.0),
        ),
        child: Text(
          widget.episode['name']!,
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 12,
            fontWeight: widget.selected ? FontWeight.bold : FontWeight.normal,
          ),
        ),
      ),
    );
  }
}
